package items;

import java.util.Random;

import elements.Element;
import elements.Robot;

import roborally.EnergyAmount;
import roborally.EnergyUnit;
import roborally.Orientation;
import roborally.Position;
/**
 * @version	3.0
 * @author 	Lies Tambeur 	r0264469	2Bcwsbbh
 * @author	Eline Vanrykel	r0264543	2Bcwselt
 *
 */
public class SurpriseBox extends Item{

	/**
	 * @param weight
	 * @effect	| super(weight)
	 */
	public SurpriseBox(int weight) {
		super(weight);
	}
	
	/**
	 * @effect	| for each orientation in Orientation.values():
	 * 			| 	for each element in this.getPosition.getNeighbour(orientation).getElements():
	 * 			|		element.hit()
	 */
	@Override
	public void hit() {
		for(Orientation orient : Orientation.values()){
			Position pos = this.getPosition().getNeighbour(orient);
			if(this.getBoard().isPositionOnBoard(pos) && !this.getBoard().getElements(pos).isEmpty()){
				java.util.Iterator<Element> it = this.getBoard().getElements(pos).iterator();
				it.next().hit();
			}
		}
	}
	
	/**
	 * @effect	| robot.removeItem(this) && this.terminate()
	 * @effect	| robot.hit() || robot.teleport() 
	 * 			| 	|| ( robot.addItem( randomBattery || randomRepairKit || randomSupriseBox ) 
	 * 			|			with randomItem an item with randomly created values that are required for the constructor of that specific item  )
	 */
	@Override
	public void used(Robot robot) {
		Random random = new Random();
		//int event = random.nextInt(3);
		int event = 1;
		if(event == 0){
			robot.hit();
			System.err.println("Surprise box exploded and the robot is hit by a laser!");
		} 
		else if(event == 1){
			robot.teleport();
			System.err.println("Robot teleported to position "+(robot.getPosition().toString()));
		} else if(event == 2){
			System.err.print("New item added to posessions: ");
			int typeOfItem = random.nextInt(3);
			Item item = null;
			if(typeOfItem == 0){
				item = new Battery(new EnergyAmount(random.nextDouble()*new EnergyAmount(5000,EnergyUnit.WATTSECONDS).toEnergyUnit(EnergyUnit.WATTSECONDS).getAmount(),EnergyUnit.WATTSECONDS), random.nextInt(5000));
				System.err.println("BATTERY with weight: "+((Battery)item).getWeight()+", energy "+((Battery)item).getEnergy());
			} else if (typeOfItem == 1){
				item = new RepairKit(random.nextInt(5000), 
							new EnergyAmount(random.nextDouble()*RepairKit.getMaxEnergy().toEnergyUnit(EnergyUnit.WATTSECONDS).getAmount(),EnergyUnit.WATTSECONDS));
				System.err.println("REPAIRKIT with weight: "+((RepairKit)item).getWeight()+", energy "+((RepairKit)item).getEnergy());
			} else if (typeOfItem == 2){
				item = new SurpriseBox(random.nextInt(5000));
				System.err.println("SURPRISEBOX with weight: "+((SurpriseBox)item).getWeight());
			}
			robot.addItem(item);
		}
		robot.removeItem(this);
		this.terminate();
	}
	
	/**
	 * @return	| result == "SURPRISE BOX with weight "+this.getWeight()+"g"
	 */
	@Override
	public String toString(){
		return "SURPRISE BOX with weight "+this.getWeight()+"g";
	}

}
